package net.keebook.webapp.dao;

import java.util.ArrayList;
import java.util.List;

import javax.ejb.Stateless;
import javax.inject.Inject;
import javax.persistence.Query;

import net.keebook.webapp.entity.PasswordItem;


@Stateless
public class PasswordDao extends Dao<PasswordItem> {
	@Inject
	private KeeBookDao keeBookDao;
	
	@Override
	public void store(PasswordItem item) {
		super.store(item);
	}
	
	@Override
	public void update(PasswordItem item) {
		super.update(item);
	}
	
	@Override
	public PasswordItem get(Long id) {
		return super.get(id);
	}
	
	@Override
	public PasswordItem get(String id) {
		return super.get(id);
	}
	
	@Override
	public void delete(PasswordItem item) {
		super.delete(item);
	}
	
	@SuppressWarnings("unchecked")
	public List<String> getSuggestUsernames(Long userId, Long keeBookId) {
		Query query = getEntityManager().createQuery("SELECT p.username FROM KeeBookAccess a, PasswordItem p WHERE p.keeBook.id = a.pk.keeBook.id AND a.pk.user.id = :userId AND a.pk.keeBook.id = :keeBookId AND p.username != '' ORDER BY p.title");
		query.setParameter("userId", userId);
		query.setParameter("keeBookId", keeBookId);
		return (List<String>)query.getResultList();
	}
	
	@SuppressWarnings("unchecked")
	public List<String> getSuggestKeywords(Long userId, Long keeBookId, String keyword) {
		Query query = getEntityManager().createQuery("SELECT p.title FROM KeeBookAccess a, PasswordItem p WHERE p.keeBook.id = a.pk.keeBook.id AND a.pk.user.id = :userId AND a.pk.keeBook.id = :keeBookId AND p.title LIKE :keyword ORDER BY p.title");
		query.setParameter("userId", userId);
		query.setParameter("keeBookId", keeBookId);
		query.setParameter("keyword", keyword+"%");
		return (List<String>)query.getResultList();
	}
	
	@SuppressWarnings("unchecked")
	public List<PasswordItem> getList(Long userId, Long keeBookId) {
		if (!getKeeBookDao().canAccess(userId, keeBookId)) {
			return new ArrayList<PasswordItem>();
		}
		Query query = getEntityManager().createQuery("FROM PasswordItem p WHERE p.keeBook.id = :keeBookId ORDER BY p.title");
		query.setParameter("keeBookId", keeBookId);
		return query.getResultList();
	}
	
	@SuppressWarnings("unchecked")
	public List<PasswordItem> getList(Long userId, Long keeBookId, String keyword, String groupId, String excludeId) {
		if (!getKeeBookDao().canAccess(userId, keeBookId)) {
			return new ArrayList<PasswordItem>();
		}
		String conditions = "";
		if (keyword != null && !keyword.isEmpty()) {
			conditions += " AND p.title LIKE :keyword ";
		}
		if (groupId != null && !"".equals(groupId)) {
			conditions += " AND p.group.exposableId = :groupId ";
		}
		if (excludeId != null && !"".equals(excludeId)) {
			conditions += " AND p.exposableId != :excludeId ";
		}
		//Query query = getEntityManager().createQuery("SELECT p FROM KeeBookAccess a, PasswordItem p WHERE p.keeBook.id = a.pk.keeBook.id AND a.pk.keeBook.id = :keeBookId AND a.pk.user.id = :userId "+conditions+" ORDER BY p.title");
		Query query = getEntityManager().createQuery("FROM PasswordItem p WHERE p.keeBook.id = :keeBookId "+conditions+" ORDER BY p.favored DESC, p.title");
		//query.setParameter("userId", userId);
		query.setParameter("keeBookId", keeBookId);
		if (keyword != null && !keyword.isEmpty()) {
			query.setParameter("keyword", "%"+keyword+"%");
		}
		if (groupId != null && !"".equals(groupId)) {
			query.setParameter("groupId", groupId);
		}
		if (excludeId != null && !"".equals(excludeId)) {
			query.setParameter("excludeId", excludeId);
		}
		return query.getResultList();
	}
	
	public boolean canAccess(Long userId, Long itemId) {
		Query query = getEntityManager().createQuery("SELECT p FROM KeeBookAccess a, PasswordItem p WHERE p.keeBook.id = a.pk.keeBook.id AND p.id = :itemId AND a.pk.user.id = :userId");
		query.setParameter("itemId", itemId);
		query.setParameter("userId", userId);
		query.setMaxResults(1);
		return !query.getResultList().isEmpty();
	}

	public KeeBookDao getKeeBookDao() {
		return keeBookDao;
	}

	public void setKeeBookDao(KeeBookDao keeBookDao) {
		this.keeBookDao = keeBookDao;
	}
}
